import '../models/adress_info_model.dart';
import '../manger/network_utils.dart';

// 封装返回结果
class LoadAdressResult {
  final bool success;
  final List<AdressInfo>? adressInfo;
  LoadAdressResult({required this.success, this.adressInfo});
}

class DeleteAdressResult {
  final bool success;
  DeleteAdressResult({required this.success});
}

class LoadQucikCardResult {
  final bool success;
  final List<CardInfo>? cardInfo;
  LoadQucikCardResult({required this.success, this.cardInfo});
}

class DeleteQuickCardResult {
  final bool success;
  DeleteQuickCardResult({required this.success});
}

// 单例管理器
class AdressInfoManager {
  static final AdressInfoManager _instance = AdressInfoManager._internal();
  factory AdressInfoManager() => _instance;
  AdressInfoManager._internal();

  // 全局数据
  List<AdressInfo>? _adressListModel;
  // 获取全局数据方法
  List<AdressInfo>? get adressListModel => _adressListModel;

    // 全局数据
  List<CardInfo>? _quickCardList;
  // 获取全局数据方法
  List<CardInfo>? get quickCardList => _quickCardList;

  // 加载地址信息
  Future<LoadAdressResult> loadInfoData() async {
    try {
      final response = await NetworkUtils.get(
        RequestCmd.address,
        params: null,
      );

      final model = AdressModel.fromJson(response);

      if (model.code == 200) {
        _adressListModel = model.data;
        return LoadAdressResult(success: true, adressInfo: model.data);
      } else {
        return LoadAdressResult(success: false);
      }
    } catch (e) {
      return LoadAdressResult(success: false);
    }
  }

  // 删除地址
  Future<DeleteAdressResult> deletteAddress(String addressId) async {
    try {
      final response = await NetworkUtils.delete(        
        RequestCmd.address,
        addressId,
        headers: null,
      );

      if (response['code'] == 200) {
        return DeleteAdressResult(success: true);
      } else {
        return DeleteAdressResult(success: false);
      }
    } catch (e) {
      return DeleteAdressResult(success: false);
    }
  }

  // 请求快捷卡列表
  Future<LoadQucikCardResult> loadCardList() async {
    try {
      final response = await NetworkUtils.post(
        RequestCmd.quickPayment,
        body: null,
      );

      final model = CardModel.fromJson(response);

      if (model.code == 200) {
        _quickCardList = model.data;
        return LoadQucikCardResult(success: true, cardInfo: model.data);
      } else {
        return LoadQucikCardResult(success: false);
      }
    } catch (e) {
      return LoadQucikCardResult(success: false);
    }
  }
 
  // 删除快捷卡
  Future<DeleteQuickCardResult> deletteCard(String cardId) async {
    try {
      final response = await NetworkUtils.post(
        RequestCmd.delCard,
        body: {"id":cardId},
      );

      final model = CardModel.fromJson(response);

      if (model.code == 200) {
        _quickCardList = model.data;
        return DeleteQuickCardResult(success: true);
      } else {
        return DeleteQuickCardResult(success: false);
      }
    } catch (e) {
      return DeleteQuickCardResult(success: false);
    }
  }
}
